package cn.it.shop.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import cn.it.shop.model.Product;
import cn.it.shop.service.ProductService;

@SuppressWarnings("unchecked")
@Service("productService")
public class ProductServiceImpl extends BaseServiveImpl<Product> implements
		ProductService {

	@Override
	public List<Product> queryJoinCategory(String name, int page, int size) {
		String hql = "FROM Product p LEFT JOIN FETCH p.category WHERE p.name LIKE :name ";
		return getSession().createQuery(hql).setMaxResults(size)
				.setFirstResult((page - 1) * size)
				.setString("name", "%" + name + "%").list();
	}

	@Override
	public long getCount(String name) {
		String hql = "SELECT count(p) FROM Product p WHERE p.name LIKE :name ";
		return (Long) getSession().createQuery(hql)
				.setString("name", "%" + name + "%").uniqueResult();
	}

	@Override
	public void deleteByIds(String ids) {
		String hql = "DELETE Product p WHERE p.id in (" + ids + ")";
		getSession().createQuery(hql).executeUpdate();

	}

	@Override
	public List<Product> queryByCid(int cid) {
		String hql = "FROM Product p JOIN FETCH p.category WHERE p.commend=true AND p.open=true AND p.category.id=:cid ORDER BY p.date ASC";
		return getSession().createQuery(hql)//
				.setFirstResult(1).setMaxResults(4)//
				.setInteger("cid", cid)//
				.list();//
	}
}
